from appium.webdriver.common.touch_action import TouchAction

from utils.base_utils import driver,swipe_up
from pages.login_page import LoginPage
from pages.set_page import SetPage
from time import sleep

class LoginLogic():


    # 实现登录操作
    def login(self,driver,username, password):

        # 1. 点击 我
        driver.find_element(*LoginPage.my_button).click()
        sleep(1)
        # 2. 点击 登录
        driver.find_element(*LoginPage.login_link_button).click()
        sleep(1)
        # 3. 点击 密码登录
        driver.find_element(*LoginPage.login_type_is_password_button).click()
        # 4. 输入手机号
        driver.find_element(*LoginPage.mobile_input_text).send_keys(username)
        # 5. 输入密码
        driver.find_element(*LoginPage.password_input_text).send_keys(password)
        # 6. 选择复选框
        # driver.find_element(*LoginPage.login_check_box).click()
        TouchAction(driver).tap(x=LoginPage.login_check_box[0],y=LoginPage.login_check_box[1]).perform()
        # 7. 点击登录
        driver.find_element(*LoginPage.login_button).click()


    # 获取登录成功的信息 ： 昵称
    def get_login_info(self,driver):
        return driver.find_element(*LoginPage.nick_name_text).text

    # 实现登录退出
    def logout(self,driver):
        # 1 .点击设置
        # driver.find_element(*SetPage.set_button).click()
        TouchAction(driver).tap(x=SetPage.set_button[0],y=SetPage.set_button[1]).perform()
        sleep(1)
        # 2. 点击退出
        swipe_up(driver)
        driver.find_element(*SetPage.logout_button).click()
        sleep(0.5)
        # 3. 点击确定按钮
        driver.find_element(*SetPage.confim_button).click()

    # 获取退出后登录信息
    def get_logout_info(self,driver):
        return driver.find_element(*SetPage.logout_info).text




if __name__ == '__main__':
    driver = driver()
    login = LoginLogic()
    login.login(driver,"15548816278","a123456")